package org.itzixi.service;

import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.itzixi.RedisKeyConstant;
import org.itzixi.api.feign.FileFein;
import org.itzixi.enums.PlatformEnum;
import org.itzixi.enums.RedisKeyEnum;
import org.itzixi.excptions.GraceException;
import org.itzixi.grace.result.GraceJSONResult;
import org.itzixi.grace.result.ResponseStatusEnum;
import org.itzixi.mapper.UsersMapper;
import org.itzixi.pojo.Users;
import org.itzixi.pojo.bo.ModifyUserBO;
import org.itzixi.pojo.vo.UsersVO;
import org.itzixi.utils.RedisOperator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 顾文龙
 * @since 2024-06-12
 */
@Service
public class UsersService{

    @Resource
    private UsersMapper usersMapper;


    @Resource
    private RedisOperator redisOperator;

    @Resource
    private FileFein fileFein;

    public void modifyUserinfo(ModifyUserBO modifyUserBO){
        Users users = new Users();

        String userId = modifyUserBO.getUserId();
        String wechatNum = modifyUserBO.getWechatNum();
        if (StringUtils.isBlank(userId))
            GraceException.display(ResponseStatusEnum.USER_INFO_UPDATED_ERROR);
        if (StringUtils.isNotBlank(wechatNum)){
            String isExist = redisOperator.get(RedisKeyConstant.USER_ALREADY_UPDATE_WECHAT_NUM + userId);
            if (StringUtils.isNotBlank(isExist)){
                GraceException.display(ResponseStatusEnum.WECHAT_NUM_ALREADY_MODIFIED_ERROR);
            } else {
                // 修改微信二维码
                String qrCodeUrl = getQrCodeUrl(wechatNum, userId);
                users.setWechatNumImg(qrCodeUrl);
            }
        }
        users.setId(userId);
        users.setUpdatedTime(LocalDateTime.now());

        BeanUtils.copyProperties(modifyUserBO, users);
        usersMapper.updateById(users);


        if (StringUtils.isNotBlank(wechatNum)){
            redisOperator.setByDays(RedisKeyConstant.USER_ALREADY_UPDATE_WECHAT_NUM + userId, userId, 365 );
        }

    }


    private String getQrCodeUrl(String wechatNum, String userId){
        GraceJSONResult<String> result = fileFein.generatorQrCode(wechatNum, userId);
        String qrCodeUrl = result.getData();
        return qrCodeUrl;
    }


    public UsersVO getUserInfoById(String userId, boolean needToken){
        Users users = usersMapper.findUserInfoById(userId);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users, usersVO);
        if (needToken){
            String userAppToken = PlatformEnum.TOKEN_APP_PREFIX.getType() + StrUtil.DOT + UUID.randomUUID().toString();
            redisOperator.set(RedisKeyEnum.REDIS_USER_TOKEN.getKey() + userAppToken, users.getId());
            usersVO.setUserToken(userAppToken);
        }
        return usersVO;
    }

    /**
     * 根据手机号或者微信号查询微信号
     * @param queryString
     * @return
     */
    public Users getByWechatNumOrMobile(String queryString){
        return usersMapper.findByWechatNumOrMobile(queryString);
    }

}
